package eerbaar.v2.app.core;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JPanel;

import eerbaar.v2.app.util.Keyboard;
import eerbaar.v2.graphics.IMainWindow;
import eerbaar.v2.graphics.MainWindowFactory;

public class MainWindow implements IMainWindow {

    private final Container frame;
    private final RightPanel rightPanel = new RightPanel();

    public MainWindow(Container container) {
        this.frame = container;
        MainWindowFactory.setMainWindow(this);
    }

    public Container getFrame() {
        return this.frame;
    }

    public Container getPanel() {
        return this.frame;
    }

    public RightPanel getRightPanel() {
        return this.rightPanel;
    }

    public void init(Container container) {
        Container panel = this.frame;
        panel.setPreferredSize(new Dimension(750, 510));
        panel.setLayout(null);

        Keyboard.init(this.frame);

        loadTitle();

        this.rightPanel.getPanel().setBounds(520, 10, 300, 70);
        panel.add(this.rightPanel.getPanel());

        setupLayout(panel, null, this.rightPanel.getPanel());
    }

    private void loadTitle() {

        String title = "eerbaar ";

        InputStream in = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("version.txt");
        if (in != null) {

            java.io.BufferedReader reader = new java.io.BufferedReader(
                    new java.io.InputStreamReader(in));
            String line;
            try {

                line = reader.readLine();
                title += line;
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        // this.frame.setTitle(title);
    }

    public void setIconImage(Image image) {
        // this.frame.setIconImage(image);
    }

    private void setupLayout(Container container, JPanel panelLeft,
            JPanel panelRight) {

        // container.setLayout(new java.awt.GridBagLayout());
        container.setLayout(null);
        // panelLeft.setBounds(0, 100, 500, 500);
        panelRight.setBounds(510, 0, 250, 500);

        // GridBagConstraints gridBagConstraints = new GridBagConstraints();
        // gridBagConstraints.gridx = 0;
        // gridBagConstraints.gridy = 0;
        // gridBagConstraints.fill = GridBagConstraints.BOTH;
        // gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        // gridBagConstraints.weightx = 1.0;
        // gridBagConstraints.weighty = 1.0;
        // container.add(panelLeft, gridBagConstraints);
        //
        // gridBagConstraints = new GridBagConstraints();
        // gridBagConstraints.gridx = 1;
        // gridBagConstraints.gridy = 0;
        // gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
        // gridBagConstraints.gridheight = GridBagConstraints.REMAINDER;
        // gridBagConstraints.fill = GridBagConstraints.BOTH;
        // gridBagConstraints.anchor = GridBagConstraints.NORTH;
        // gridBagConstraints.weightx = 1.0;
        // gridBagConstraints.weighty = 1.0;
        // container.add(panelRight, gridBagConstraints);
    }

}
